package org.starsource.forum.server.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Security {
	private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
	.getLogger(Security.class.getName());

	private static final String MD5_KEY = "93bdebb0-6959-4f80-9859-94bf3f57a118";
	public static String getMD5(String source) {
		try {
			String result = null;
			char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
			MessageDigest md = MessageDigest.getInstance( "MD5" );
			md.update( (MD5_KEY + source).getBytes() );
			byte tmp[] = md.digest(); 

			char str[] = new char[16 * 2];
			int k = 0;
			for (int i = 0; i < 16; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			result = new String(str);
			logger.debug("MD5=" + result);
			return result;
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getStackTrace());
			return source;
		}
	}
}
